/****************************************************************************
 * Copyright (C) 2011 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 *     Description: MAXQ FIFO driver
 *             $Id: maxq_fifo.c 1961 2011-08-25 14:47:00Z jbrodt $
 *
 ********************************************************************************
 */

#include "maxq_config.h"
#include "maxq_fifo.h"

/***** Definitions *****/
#define INT_GLOBAL_ENABLE   0x01  // IC register bit mask


/***** Function Prototypes *****/


/***** Global Data *****/


/****************************************************************************/
void fifo_init(fifo_t * fifo, void * mem, unsigned int length)
{
  uint8_t istate;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  fifo->rindex = 0;
  fifo->windex = 0;
  fifo->data = mem;
  fifo->length = length;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state
}

/****************************************************************************/
int fifo_put8(fifo_t * fifo, uint8_t element)
{
  uint8_t istate;

  // Check if FIFO is full
  if ( (fifo->windex == (fifo->rindex - 1)) ||
       ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1))) )
    return MAXQ_FAILURE;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  // Put data into FIFO
  ((uint8_t*)(fifo->data))[fifo->windex] = element;

  // Increment pointer
  fifo->windex++;
  if (fifo->windex == fifo->length)
    fifo->windex = 0;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state
    
  return MAXQ_SUCCESS;
}

/****************************************************************************/
int fifo_get8(fifo_t * fifo, uint8_t * element)
{
  uint8_t istate;

  // Check if FIFO is empty
  if (fifo->rindex == fifo->windex)
    return MAXQ_FAILURE;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

   // Get data from FIFO
  *element = ((uint8_t*)(fifo->data))[fifo->rindex];

  // Increment pointer
  fifo->rindex++;
  if (fifo->rindex == fifo->length)
    fifo->rindex = 0;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state
    
  return MAXQ_SUCCESS;
}

/****************************************************************************/
int fifo_put16(fifo_t * fifo, uint16_t element)
{
  uint8_t istate;

  // Check if FIFO is full
  if ( (fifo->windex == (fifo->rindex - 1)) ||
       ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1))) )
    return MAXQ_FAILURE;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  // Put data into FIFO
  ((uint16_t*)(fifo->data))[fifo->windex] = element;

  // Increment pointer
  fifo->windex++;
  if (fifo->windex == fifo->length)
    fifo->windex = 0;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state
    
  return MAXQ_SUCCESS;
}

/****************************************************************************/
int fifo_get16(fifo_t * fifo, uint16_t * element)
{
  uint8_t istate;

  // Check if FIFO is empty
  if (fifo->rindex == fifo->windex)
    return MAXQ_FAILURE;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  // Get data from FIFO
  *element = ((uint16_t*)(fifo->data))[fifo->rindex];

  // Increment pointer
  fifo->rindex++;
  if (fifo->rindex == fifo->length)
    fifo->rindex = 0;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state
    
  return MAXQ_SUCCESS;
}

/****************************************************************************/
void fifo_clear(fifo_t * fifo)
{
  uint8_t istate;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  fifo->rindex = 0;
  fifo->windex = 0;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state
}

/****************************************************************************/
int fifo_empty(fifo_t * fifo)
{
  if (fifo->rindex == fifo->windex)
    return TRUE;

  return FALSE;
}

/****************************************************************************/
int fifo_full(fifo_t * fifo)
{
  uint8_t istate;
  int retval = FALSE;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  if ( (fifo->windex == (fifo->rindex - 1)) ||
       ((fifo->rindex == 0) && (fifo->windex == (fifo->length - 1))) )
    retval = TRUE;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state

  return retval;
}

/****************************************************************************/
unsigned int fifo_level(fifo_t * fifo)
{
  uint16_t value;
  uint8_t istate;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  if (fifo->windex > fifo->rindex)
    value = fifo->windex - fifo->rindex;
  else
    value = fifo->length - fifo->rindex + fifo->windex;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state

  return value;
}

/****************************************************************************/
unsigned int fifo_remaining(fifo_t * fifo)
{
  uint16_t value;
  uint8_t istate;

  // atomic FIFO access
  istate = IC;  // save interrupt enabled state
  IC &= ~INT_GLOBAL_ENABLE;  // disable interrupts globally

  if (fifo->rindex > fifo->windex)
    value = fifo->rindex - fifo->windex - 1;
  else 
    value = fifo->length - fifo->windex + fifo->rindex - 1;

  IC |= (istate & INT_GLOBAL_ENABLE);  // restore interrupt enabled state

  return value;
}
